package hichecker.ui.general_views.toolbars;

import hichecker.controller.Config;
import hichecker.controller.MainController;
import hichecker.tools.event.HiCheckerEvent;
import hichecker.tools.event.abstract_event.Observable;
import hichecker.tools.event.abstract_event.Observer;
import hichecker.ui.MainFrame;
import hichecker.ui.components.icon_factory.IconFactory;
import hichecker.ui.components.icon_factory.IconFactory.IconType;
import hichecker.university.ImportExportCourse;
import hichecker.university.Series;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.Box;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JToolBar;

/**
 * Class for the menu bar represent at the top of the main frame.
 */
public class MenuBar extends JToolBar implements Observer<HiCheckerEvent> {
	private MainController controller;
	private JButton newButton, openButton, switchButton, saveButton, saveAsButton, importButton,
			exportButton, csvButton;

	public MenuBar(final MainFrame frame, MainController controller) {
		this.controller = controller;

		InputMap inputMap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		ActionMap actionMap = this.getActionMap();

		// NEW - Button
		AbstractAction newAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.newFile();
			}
		};
		newButton = IconFactory.getIconButton(null, Config.MENU_NEW, IconType.NEW, newAction);
		inputMap.put(Config.HOTKEY_NEW, "new");
		actionMap.put("new", newAction);

		// OPEN - Button
		AbstractAction openAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.openFile();
			}
		};
		openButton = IconFactory.getIconButton(null, Config.MENU_OPEN, IconType.OPEN, openAction);
		inputMap.put(Config.HOTKEY_OPEN, "open");
		actionMap.put("open", openAction);

		// SAVE - Button
		AbstractAction saveAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.saveFile();
			}
		};
		saveButton = IconFactory.getIconButton(null, Config.MENU_SAVE, IconType.SAVE, saveAction);
		inputMap.put(Config.HOTKEY_SAVE, "save");
		actionMap.put("save", saveAction);

		// SAVE AS - Button
		AbstractAction saveAsAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.saveFileAs();
			}
		};
		saveAsButton = IconFactory.getIconButton(null, Config.MENU_SAVE_AS, IconType.SAVE_AS, saveAsAction);
		inputMap.put(Config.HOTKEY_SAVE_AS, "saveAs");
		actionMap.put("saveAs", saveAsAction);

		// IMPORT - Button
		AbstractAction importAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.importFile();
			}
		};
		importButton = IconFactory.getIconButton(null, Config.MENU_IMPORT, IconType.IMPORT_PAGE, importAction);
		inputMap.put(Config.HOTKEY_IMPORT, "import");
		actionMap.put("import", importAction);

		// EXPORT - Button
		AbstractAction exportAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.exportFile();
			}
		};
		exportButton = IconFactory.getIconButton(null, Config.MENU_EXPORT, IconType.EXPORT_PAGE, exportAction);
		inputMap.put(Config.HOTKEY_EXPORT, "export");
		actionMap.put("export", exportAction);


		// CSV - Button
		AbstractAction csvAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.saveAsCSV();
			}
		};
		csvButton = IconFactory.getIconButton("CSV", Config.MENU_CSV, IconType.SAVE_AS_CSV, csvAction);

		// SWITCH - Button
		AbstractAction switchAction = new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.switchViewSettings();
			}
		};
		switchButton = IconFactory.getIconButton(null, Config.MENU_SWITCH, IconType.SWITCH, switchAction);
		inputMap.put(Config.HOTKEY_SWITCH, "switch");
		actionMap.put("switch", switchAction);


		// disable inactive buttons
		saveButton.setEnabled(false);
		saveAsButton.setEnabled(false);
		importButton.setEnabled(false);
		exportButton.setEnabled(false);
		switchButton.setEnabled(false);

		// TOOLBAR
		setFloatable(false);

		// add only if user has full access
		if (controller.hasFullAccess()) {
			add(newButton);
		}

		// standard buttons
		add(saveButton);
		add(saveAsButton);
		add(openButton);
		//add(csvButton);

		// add only if user has full access
		if (controller.hasFullAccess()) {
			addSeparator();
			add(importButton);
			add(exportButton);
			add(Box.createHorizontalGlue());
			add(switchButton);
		}
	}

	@Override
	public void notify(Observable<? extends HiCheckerEvent> sender, HiCheckerEvent event) {
		// Disable inactive symbols
		boolean enabled = controller.getCourse() != null;
		switchButton.setEnabled(enabled);
		saveButton.setEnabled(enabled);
		saveAsButton.setEnabled(enabled);
		importButton.setEnabled(enabled);

		exportButton.setEnabled(controller.getViewedElement() instanceof Series);
		switchButton.setEnabled(!(controller.getViewedElement() instanceof ImportExportCourse));
	}
}
